package leetcode;

/**
 * @ProjectName: javalearn
 * @Package: PACKAGE_NAME
 * @ClassName: strStr
 * @Description: java类作用描述
 * @Author: 唐朝
 * @CreateDate: 2019/9/15 13:14
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/15 13:14
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class strStr {
    public static int strStr(String haystack, String needle) {
        char[] a = haystack.toCharArray();
        char[] b = needle.toCharArray();

        if (b.length == 0 ){
            return 0;
        }

        int r = 0;

        for (int i = 0; i < a.length; i++){
            if (a[i] == b[r]){
                r++;
            }else {
                if (r != 0){
                    i = i - r;
                    r = 0;
                }
            }
            if (r == b.length){
                return i - r + 1;
            }
        }
        return -1;

    }

    public static int strStr1(String haystack, String needle) {

        if (needle.length() == 0 ){
            return 0;
        }

        int r = 0;

        for (int i = 0; i < haystack.length(); i++){
            if (haystack.charAt(i) == needle.charAt(r)){
                r++;
            }else {
                if (r != 0){
                    i = i - r;
                    r = 0;
                }
            }
            if (r == needle.length()){
                return i - r + 1;
            }
        }
        return -1;

    }

    public static void main (String[] args){
        System.out.println(strStr("aaaa", "a"));
        System.out.println(strStr("ississsiss", "isssi"));
        System.out.println(strStr("ississsissppi", "pi"));

        int[] a = new int[3];
        System.out.println(a[0]++);
        System.out.println(a[0]);
    }
}
